<?php
	include "header.php";
	$userid = $_COOKIE['userid'];

	$redis = connect_redis();
	$username = $_GET['uname'] ? $_GET['uname'] : 'XXX';
	$curren_uid = $redis->get("user:userid:username".$username);

	// 查询我的关注集合
	$is_followed = $redis->sIsMember("follows:userid".$userid,$curren_uid);

	// 判断当前主页是否自己
	$is_me = $userid == $curren_uid;

	// 获取当前用户的动态列表
	$curr_daynic_list = $redis->zRevRange("userweibo:list:userid".$curren_uid,0,-1);
	// $res = $redis->sort("userweibo:list:userid".$curren_uid,array("sort"=>"desc","hget"=>"weibo:data:weiboid*,content"));
	// var_dump($res);
	// die;
	
	$curr_weibo_list = [];
	foreach($curr_daynic_list as $key=>$value) {
		$curr_weibo_list[] = [
			"id" => $value,
			"userid" => $curren_uid,
			"content" => $redis->hget("weibo:data:weiboid".$value,"content"),
			"create_time" => $redis->hget("weibo:data:weiboid".$value,"create_time")
		];
	}
	// var_dump($curr_weibo_list);
	// die;

	// var_dump($userid);
	// die;

	// $page = isset($_GET['page']) ? intval($_GET['page']) : 0;					// 开始页
	// $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : 10;		// 页码

	// $redis = connect_redis();
	// $mysend_list = $redis->zRevRange("userweibo:list:userid".$userid,0,-1);			// 获取我所有动态

	// $weibo_list = array();
	// foreach ($mysend_list as $key => $value) {
	// 	$weibo_list[] = [
	// 		"weibo_id" => $value,
	// 		"userid" => $userid,
	// 		"content" => $redis->hget("weibo:data:weiboid".$value,'content'),
	// 		"create_time" => best_time($redis->hget("weibo:data:weiboid".$value,'create_time')),
	// 	];
	// }

?>
<style>
	.mysend_list {
		margin-top:35px;
	}
	.mysend_list ul li {margin-bottom:15px;border-bottom:#ccc 1px dashed;padding:6px 2px;}
	.mysend_list ul li p {font-size:14px;color:#666;margin-top:3px;}

	.follow_btn {padding:7px 14px;background:green;border-radius:5px;color:white;text-decoration:none;}
</style>
<div id="content">
	<!-- <h1>我这里是主页</h1> -->
	<div class="publish">
		<h2 style="padding-bottom:20px">欢迎进入 <span style="color:red"><?php echo $username ?></span> 的主页</h2>
		
		<?php
			if(!$is_me) {
				if(!$is_followed) {
					echo "<a href=\"action.php?type=follow&stas=follow&userid=".$curren_uid."\" class=\"follow_btn\">关注</a>";
				}else {
					echo "<a href=\"action.php?type=follow&stas=unfollow&userid=".$curren_uid."\" class=\"follow_btn\">取消关注</a>";
				}
			} 
		?> 
		<!-- <a href="action.php?type=follow" class="follow_btn">关注</a> -->

	</div>
	
	<div class="mysend_list">
		<h3 style="margin-bottom:5px;">TA的最新动态</h3>
		<ul>
			<?php 
				if(empty($curr_weibo_list)) {
					echo "TA还没有发表任何动态哦~";
				}else {

					foreach ($curr_weibo_list as $key => $value) {
						echo '<li>'.
								$value['content'].
								'<p>'.best_time($value['create_time']).'</p>'.
								'<p>'.
									'<a href="action.php?type=delWeibo&weibo_id">评论</a> &nbsp;'.
									'<a href="action.php?type=delWeibo&weibo_id">点赞</a>'.
								'</p>'.
							'</li>';	
					}

				}
			?>

			<!-- <li>
				我今天没做啥啊
				<p>6天前</p>
				<p>
					<a href="action.php?type=delWeibo&weibo_id">评论</a>
					<a href="action.php?type=delWeibo&weibo_id">加锁</a>
				</p>
			</li> -->

			<!-- <li>
				我今天没做啥啊
				<p>6天前</p>
			</li> -->
		</ul>
	</div>

</div>

			
<?php
include "footer.php";
?>

